package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.MyCredit;
import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.mapper.MyCreditMapper;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.service.ISignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class SignServiceImpl implements ISignService {
    @Autowired
    private SignMapper signMapper;
    @Autowired
    private MyCreditMapper creditMapper;
    @Override
    public Sign achieve(Long useId) {
        if (signMapper.selectByPrimaryKey(useId) != null) {
            //怎样判断是否是连续签到,单独查询时间如果间隔时间大于一天那就说明间隔签到就改变次数(在查的时候就获取具体信息)
            signMapper.catchStateByDate();
            int len = (int)signMapper.catchTimesByUserId(useId);

            return signMapper.selectByPrimaryKey(useId);
        }else {
            ////如果是第一次进入点签到时(新增操作,初始化参数)
            Sign sign = new Sign();
            sign.setUserId(useId);
            sign.setTimes(0L);
            sign.setSignstate(false);
            signMapper.insert(sign);

            MyCredit myCredit = sign.getMyCredit();
            myCredit.setId(useId);
            myCredit.setCredit(0L);
            creditMapper.insert(myCredit);
            return signMapper.selectByPrimaryKey(useId);
        }

    }

    @Override
    public void renew(Sign sign) {
        //怎样判断是否是连续签到,单独查询时间如果间隔时间大于一天那就说明间隔签到就改变次数


        sign.setSigndate(new Date());
        signMapper.updateByPrimaryKey(sign);

        //更新积分
        MyCredit myCredit = sign.getMyCredit();
        myCredit.setId(sign.getUserId());
        myCredit.setCredit(10L);
        creditMapper.updateByPrimaryKey(myCredit);
        System.out.println("-------------------------------------------");






    }
}
